-- return {
-- 	"williamboman/mason.nvim",
-- 	dependencies = {
-- 		"williamboman/mason-lspconfig.nvim",
-- 		"neovim/nvim-lspconfig",
-- 		"folke/neoconf.nvim",
-- 		"folke/neodev.nvim",
-- 		"hrsh7th/cmp-nvim-lsp",
-- 		"pmizio/typescript-tools.nvim",
-- 	},
-- 	config = function()
-- 		require("neoconf").setup({})
-- 		require("neodev").setup({})
-- 		require("mason").setup()

-- 		local lspconfig = require("lspconfig")

-- 		lspconfig.gopls.setup({
-- 			settings = {
-- 				gopls = {
-- 					analyses = {
-- 						unusedparams = true,
-- 					},
-- 					staticcheck = true,
-- 					gofumpt = true,
-- 				},
-- 			},
-- 		})

-- 		local server_settings = require("ide.lsp.servers")

-- 		require("mason-lspconfig").setup({
-- 			ensure_installed = server_settings.default_servers,
-- 		})
-- 		local lspconfig = require("lspconfig")

-- 		server_settings.setup()

-- 		require("mason-lspconfig").setup_handlers({
-- 			function(server_name)
-- 				if server_settings.is_disabled(server_name) then
-- 					return
-- 				end
-- 				lspconfig[server_name].setup(server_settings.get_config(server_name))
-- 			end,
-- 		})

-- 		-- since we're using typescript-tools.nvim instead of the plain tsserver
-- 		-- it can't be installed by mason and thus needs special handling
-- 		if not server_settings.is_disabled("tsserver") then
-- 			require("typescript-tools").setup(server_settings.get_config("tsserver"))
-- 		end

-- 		-- setup diagnostics
-- 		require("ide.lsp.diagnostics").setup()
-- 	end,
-- }

-- ===============================--
-- ===============================--
-- ===============================--

return {
	"neovim/nvim-lspconfig",
	dependencies = {
		"hrsh7th/cmp-nvim-lsp",
		"pmizio/typescript-tools.nvim",
		"williamboman/mason.nvim",
		"williamboman/mason-lspconfig",
		"folke/neoconf.nvim",
		"folke/neodev.nvim",
		{
			"j-hui/fidget.nvim",
			tag = "legacy",
		},
		"nvimdev/lspsaga.nvim",
	},
	config = function()
		local servers = {
			lua_ls = {
				Lua = {
					workspace = { checkThirdParty = false },
					telemetry = { enable = false },
				},
			},
			pyright = {},
			jsonls = {},
			marksman = {},
			dockerls = {},
			docker_compose_language_service = {},
			bashles = {},
			ocamllsp = {},
			taplo = {},
			ruff_lsp = {},
			gopls = {},
			volar = {
				init_options = {
					typescript = {
						tsdk = "/usr/local/lib/node_modules//typescript/lib",
					},
				},
			},
		}
		local on_attach = function(_, bufnr)
			-- Enable completion triggered by <c-x><c-o>
			local nmap = function(keys, func, desc)
				if desc then
					desc = "LSP: " .. desc
				end
				vim.keymap.set("n", keys, func, { buffer = bufnr, desc = desc })
			end
			nmap("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
			nmap("gd", require("telescope.builtin").lsp_definitions, "[G]oto [D]efinition")
			namp("K", "<cmd>Lspsaga hover_doc<CR>", "Hover Documentation")
			namp("gi", require("telescope.builtin").lsp_implementations, "[G]oto [I]mplementation")
			nmap("<C-k>", vim.lsp.buf.signature_help, "Signature Documentation")
			nmap("<leader>wa", vim.lsp.buf.add_workspace_folder, "[W]orkspace [A]dd Folder")
			nmap("<leader>wr", vim.lsp.buf.remove_workspace_folder, "[W]orkspace [R]emove Folder")
			namp("<leader>wl", function()
				print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
			end, "[W]orkspace [L]ist Folders")
			namp("<leader>D", vim.lsp.buf.type_definition, "Type [D]efinition")
			namp("<leader>rn", "<cmd>Lspsaga rename ++project<cr>", "[R]e[n]ame")
			namp("<leader>ca", "<cmd>Lspsaga code_action<CR>", "[C]ode [A]ction")
			namp("<leader>da", require("telescope.builtin").diagnostics, "[D]i[A]gnostics")
			namp("gr", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
			nmap("<space>f", function()
				vim.lsp.buf.format({
					async = true,
				})
			end, "[F]ormat code")
		end
		require("neoconf").setup()
		require("neodev").setup()
		require("fidget").setup()
		require("lspsaga").setup()
		require("mason").setup()
		-- require("mason-lspconfig").setup({
		-- 	ensure_installed = vim.tbl_keys(servers),
		-- 	handlers = {
		-- 		function(server_name)
		-- 			require("lspconfig")[server_name].setup({
		-- 				settings = servers[server_name],
		-- 				on_attach = on_attach,
		-- 			})
		-- 		end,
		-- 	},
		-- })

		local server_settings = require("ide.lsp.servers")

		require("mason-lspconfig").setup({
			ensure_installed = server_settings.default_servers,
		})
		local lspconfig = require("lspconfig")

		server_settings.setup()

		require("mason-lspconfig").setup_handlers({
			ensure_installed = vim.tbl_keys(servers),
			function(server_name)
				if server_settings.is_disabled(server_name) then
					return
				end
				lspconfig[server_name].setup(server_settings.get_config(server_name))
			end,
		})

		-- since we're using typescript-tools.nvim instead of the plain tsserver
		-- it can't be installed by mason and thus needs special handling
		if not server_settings.is_disabled("tsserver") then
			require("typescript-tools").setup(server_settings.get_config("tsserver"))
		end

		-- setup diagnostics
		require("ide.lsp.diagnostics").setup()
	end,
}
